##--------------------------------------------------------------------------------------##
##  Replication Code for: Shauna N. Gillooly, Daniel Solomon, and Kelebogile Zvobgo.    ##
##                        "Co-Opting Truth: Explaining Quasi-Judicial Institutions in   ##
##                        Authoritarian Regimes." Human Rights Quarterly.               ##
##                                                                                      ##
##  *Figures 1-2 (main paper)                                                           ##
##                                                                                      ##
##  Prepared for Public Posting in June 2023                                            ##
##--------------------------------------------------------------------------------------##

rm(list = ls())

library(ggplot2)
library(maptools)
library(rworldmap)
library(tidyverse)
library(maps)
library(countrycode)
library(readxl)
library(reshape2)
library(foreign)

# load files
setwd("/Users/kelebogilezvobgo/Dropbox/1_Research/1_Publications/21_Co-Opting Truth/HRQ_FINAL/Data")

autoc <- read.dta("VTC_ATC_gsz2023co-opting.dta")

# give syria its own row (long format)
autoc[29,1] <- "Syria"
autoc[29,3] <- 2005
autoc$ccode <- countrycode(autoc$country, 'country.name', 'cown')

# map of autocratic TCs (total # ever by country)
autoc_dens <- aggregate(autoc$country, by = list(autoc$ccode), length) #get total number of TCs for each country

# world map shape data
map.world <- maps::map("world", ".", exact = FALSE, plot = FALSE, fill = TRUE) %>% fortify()

#merge with TC data
map.world$ccode <- countrycode(map.world$region, 'country.name', 'cown')
map.world <- merge(map.world, autoc_dens, by.x="ccode", by.y="Group.1", all.x = T)
map.world <- map.world[order(map.world$order),]
map.world$x <- ifelse(is.na(map.world$x), 0, map.world$x)


# Geographic Spread of Autocratic Truth Commissions, 1970-2018

p0<- ggplot(map.world, aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
  scale_fill_manual("ATCs", values = c('#ffffff','#bdd7e7','#6baed6','#08519c')) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/atcs.eps", width=8, height=3.5)
p0
dev.off()



tc <- read_excel("TCs and ATCs around the World gsz2023co-opting.xlsx")
tc[4:13] <- list(NULL)

# map of autocratic TCs (total # ever by country)
tc$ccode <- countrycode(tc$country_1, 'country.name', 'cown')
tc_dens <- aggregate(tc$country_1, by = list(tc$ccode), length) #get total number of TCs for each country
autoc_dens <- aggregate(tc$country_1[tc$autocratic == 1], by = list(tc$ccode[tc$autocratic == 1]), length)
names(autoc_dens)[names(autoc_dens) == 'x'] <- 'autoc'

# world map shape data
map.world <- maps::map("world", ".", exact = FALSE, plot = FALSE, fill = TRUE) %>% fortify()

# merge with TC data
map.world$ccode <- countrycode(map.world$region, 'country.name', 'cown')
map.world$geog <- ifelse(map.world$ccode < 30, "North America", 
                         ifelse(map.world$ccode < 100, "C. America and Caribbean",
                                ifelse(map.world$ccode < 200, "South America",
                                       ifelse(map.world$ccode < 400, "Europe", 
                                              ifelse(map.world$ccode < 600, "Africa",
                                                     ifelse(map.world$ccode < 700, "MENA","Asia"))))))
map.world <- merge(map.world, tc_dens, by.x="ccode", by.y="Group.1", all.x = T)
map.world <- merge(map.world, autoc_dens, by.x="ccode", by.y="Group.1", all.x = T)
map.world <- map.world[order(map.world$order),]
map.world$x <- ifelse(is.na(map.world$x), 0, map.world$x)
map.world$autoc <- ifelse(is.na(map.world$autoc), 0, map.world$autoc)


# Truth Commissions by Region
p1 <- ggplot(map.world[map.world$geog == "Africa",], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
  scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/africa tcs.eps", width=8, height=6.75)
p1
dev.off()


p2 <- ggplot(map.world[map.world$geog == "Asia" & map.world$long > 45,], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
  scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c', '#08306b'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/asia tcs.eps", width=8, height=6.75)
p2
dev.off()


# p3 <- ggplot(map.world[map.world$geog == "Europe" & map.world$long < 51,], aes( x = long, y = lat, group = group)) + 
#   geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
#   scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
#   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
#         axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
#         axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
#   coord_fixed()


p4 <- ggplot(map.world[map.world$geog == "MENA",], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
  scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/mena tcs.eps", width=8, height=6.75)
p4
dev.off()


# p5 <- ggplot(map.world[map.world$geog == "North America" & map.world$long <(-30),], aes( x = long, y = lat, group = group)) + 
#   geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
#   scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
#   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
#         axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
#         axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
#   coord_fixed()


p6 <- ggplot(map.world[map.world$geog == "C. America and Caribbean" & map.world$long <(-30),], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
  scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/c. america and caribean tcs.eps", width=8, height=6.75)
p6
dev.off()


# p7 <- ggplot(map.world[map.world$geog == "South America",], aes( x = long, y = lat, group = group)) + 
#   geom_polygon(aes(fill = as.factor(x)), color = "black", lwd = 0.1) + 
#   scale_fill_manual("TCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
#   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
#         axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
#         axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
#   coord_fixed()



# Autocratic Truth Commissions by Region

p8 <- ggplot(map.world[map.world$geog == "Africa",], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
  scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/africa atcs.eps", width=8, height=6.75)
p8
dev.off()


p9 <- ggplot(map.world[map.world$geog == "Asia" & map.world$long > 45,], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
  scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c', '#08306b'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/asia atcs.eps", width=8, height=6.75)
p9
dev.off()


# p10 <- ggplot(map.world[map.world$geog == "Europe" & map.world$long < 51,], aes( x = long, y = lat, group = group)) + 
#   geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
#   scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
#   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
#         axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
#         axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
#   coord_fixed()


p11 <- ggplot(map.world[map.world$geog == "MENA",], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
  scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/mena atcs.eps", width=8, height=6.75)
p11
dev.off()


# p12 <- ggplot(map.world[map.world$geog == "North America" & map.world$long <(-30),], aes( x = long, y = lat, group = group)) + 
#   geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
#   scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
#   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
#         axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
#         axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
#   coord_fixed()


p13 <- ggplot(map.world[map.world$geog == "C. America and Caribbean" & map.world$long <(-30),], aes( x = long, y = lat, group = group)) + 
  geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
  scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
        axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
  coord_fixed()

setEPS()
postscript("Figures/c. america and caribbean atcs.eps", width=8, height=6.75)
p13
dev.off()


# p14 <- ggplot(map.world[map.world$geog == "South America",], aes( x = long, y = lat, group = group)) + 
#   geom_polygon(aes(fill = as.factor(autoc)), color = "black", lwd = 0.1) + 
#   scale_fill_manual("ATCs", values = c('#ffffff','#6baed6','#3182bd','#08519c'), na.translate = F) +
#   theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
#         axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), 
#         axis.title.y=element_blank(), axis.text.y=element_blank(),axis.ticks.y=element_blank()) +
#   coord_fixed()